/**
 * 
 */
package travibot.engine.tasks;

import java.text.MessageFormat;

import org.apache.log4j.Logger;

import travibot.connection.ConnectionFactory;
import travibot.core.Parser;
import travibot.core.ParserFactory;
import travibot.core.VillageParser;
import travibot.engine.Task;
import travibot.model.Village;
import travibot.resource.ResourceHelper;
import travibot.utility.RequestDelayGenarator;

/**
 * @author Petr Macek
 * 
 */
public class VillageUpdateTask implements Task {

    private static Logger log = Logger.getLogger(VillageUpdateTask.class);
    
    private static final String MESSAGES_BUNDLE = "tasks";

    private ConnectionFactory connectionFactory;

    private ParserFactory parserFactory;

    private RequestDelayGenarator requestDelayGenarator;

    private Village village;

    public int compareTo(Task o) {

        return 0;
    }

    /*
     * (non-Javadoc)
     * 
     * @see travibot.engine.Task#execute()
     */
    public ExecutionResult execute() throws InterruptedException {
        ExecutionResult ret = ExecutionResult.SUCCESS;
        try {
            log.info("Refreshing village fields data: " + village.getName());
            VillageParser parserFields = (VillageParser)parserFactory.getParser(Parser.ParserType.FIELDS, connectionFactory
                    .getInputStream("dorf1.php?newdid=" + village.getId()));
            village = parserFields.getVillage(village);
    
            Thread.sleep(requestDelayGenarator.getMiliseconds());
            log.info("Refreshing village buildings data: " + village.getName());
            VillageParser parserVillage = (VillageParser)parserFactory.getParser(Parser.ParserType.CITY, connectionFactory
                    .getInputStream("dorf2.php?newdid=" + village.getId()));
            village = parserVillage.getVillage(village);
            village.setInitialized(true);
        } catch (Exception e){
            log.error(e);
            ret = ExecutionResult.FAILURE;
        }
        return ret;
    }

    /*
     * (non-Javadoc)
     * 
     * @see travibot.engine.Task#getType()
     */
    public Type getType() {

        return Task.Type.QUEUED_TASK;
    }

    public void setConnectionFactory(ConnectionFactory connectionFactory) {

        this.connectionFactory = connectionFactory;
    }

    public void setParserFactory(ParserFactory parserFactory) {

        this.parserFactory = parserFactory;
    }

    public void setRequestDelayGenarator(RequestDelayGenarator requestDelayGenarator) {

        this.requestDelayGenarator = requestDelayGenarator;
    }

    public void setVillage(Village village) {

        this.village = village;
    }

    @Override
    public String toString() {

        String message = ResourceHelper.getKeyValue(MESSAGES_BUNDLE, this.getClass().getName());
        return MessageFormat.format(message, village.getName());
    }
    
    

}
